package com.FH.organization.work_flow;

import com.FH.applyFrom.notic_form.NoticeFormResource;
import com.FH.applyFrom.notic_form.NoticeFormShowDTO;
import com.FH.organization.bpmanage.BPManageShowDTO;
import com.FH.organization.bpmanage.BPManagerResource;
import com.FH.organization.department.DepartmentResource;
import com.FH.organization.people.KamMemberResource;
import com.FH.organization.remind.RemindPullDTO;
import com.FH.organization.remind.RemindResource;
import com.FH.organization.suggestion.SuggestionPullDTO;
import com.FH.organization.suggestion.SuggestionResource;
import com.FH.organization.work_flow_history.WorkFlowHistoryPullDTO;
import com.FH.organization.work_flow_history.WorkFlowHistoryResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/7/25.
 */
@Stateless
public class WorkFlowImpl extends AccessDB implements WorkFlowDAO {
    @Inject
    KamMemberResource kamMemberResource;
    @Inject
    BPManagerResource bpManagerResource;
    @Inject
    NoticeFormResource noticeFormResource;
    @Inject
    SuggestionResource suggestionResource;
    @Inject
    RemindResource remindResource;
    @Inject
    DepartmentResource departmentResource;
    @Inject
    WorkFlowHistoryResource workFlowHistoryResource;

    @Override
    public ResultDTO getWorkFlowInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            //先查询数据库中是否存在字段
            String sqlexit = "select count(1) from work_flow where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }
            if (json.has("work_flow_approval")) {
                sql_w += " AND (work_flow_approval ='" + json.getString("work_flow_approval") + "'";
            }
            if (json.has("work_flow_approval_organization_id")) {//
                sql_w += " or work_flow_approval_organization_id ='" + json.getString("work_flow_approval_organization_id") + "')";
            }

            if (json.has("work_flow_createid")) {
                sql_w += " AND work_flow_createid ='" + json.getString("work_flow_createid") + "'";
            }

            if (json.has("work_flow_formid")) {
                sql_w += " AND work_flow_formid ='" + json.getString("work_flow_formid") + "'";
            }
            if (json.has("work_flow_type")) {
                sql_w += " AND work_flow_type ='" + json.getString("work_flow_type") + "'";
            }

            if (json.has("work_flow_state")) {
                sql_w += " AND work_flow_state ='" + json.getString("work_flow_state") + "'";
            }
            if (json.has("source_key")) {
                sql_w += " AND source_key ='" + json.getString("source_key") + "'";
            }
//            else {
////                sql_w += " AND work_flow_state IS NULL";
//                sql_w += " AND (work_flow_state IS NULL OR work_flow_state='0')";
//            }

            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<WorkFlowShowDTO> result = new ArrayList<WorkFlowShowDTO>();

            if (!existName.equals("0")) {
                String sql = "select work_flow_id,work_flow_formid,work_flow_type,work_flow_desc,work_flow_date,work_flow_order," +
                        "work_flow_createid,work_flow_approval,work_flow_suggestion,work_flow_state,work_flow_approval_organization_id," +
                        "flow_path_id,is_finish,source_key from work_flow where 1=1 " + sql_w + " ORDER BY work_flow_date DESC" + sql_page;
                //     String sql ="select * from finance_budget" ;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    WorkFlowShowDTO bp = new WorkFlowShowDTO();
                    bp.setWork_flow_id(seresult.get(i)[1]);
                    bp.setWork_flow_formid(seresult.get(i)[2]);
                    bp.setWork_flow_type(seresult.get(i)[3]);
                    bp.setWork_flow_desc(seresult.get(i)[4]);
                    bp.setWork_flow_date(seresult.get(i)[5]);
                    bp.setWork_flow_order(seresult.get(i)[6]);
                    bp.setWork_flow_createid(seresult.get(i)[7]);
                    bp.setWork_flow_approval(seresult.get(i)[8]);
                    bp.setWork_flow_suggestion(seresult.get(i)[9]);
                    bp.setWork_flow_state(seresult.get(i)[10]);
                    bp.setWork_flow_approval_organization_id(seresult.get(i)[11]);
                    bp.setFlow_path_id(seresult.get(i)[12]);
                    bp.setIs_finish(seresult.get(i)[13]);
                    bp.setSource_key(seresult.get(i)[14]);
                    ResultDTO userlist,department=null, bpmlist = null;
                    if (seresult.get(i)[8] != null && !seresult.get(i)[8].equals("")) {
                        userlist = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[8] + "}", "1", "20");
                        bp.setKamMemberShowDTOList(userlist.getValues());
                    }
                    if (seresult.get(i)[11] != null && !seresult.get(i)[11].equals("")) {
                        department = departmentResource.getDepartmentInfo("{organization_id:" + seresult.get(i)[11] + "}", "1", "20");
                        bp.setDepartmentDAOs(department.getValues());
                    }
                    bpmlist = bpManagerResource.getBPManageInfo("{table_name:" + seresult.get(i)[3] + "}", "1", "1");
                    List<BPManageShowDTO> bpManageShowDTOs = bpmlist.getValues();

                    if (seresult.get(i)[3].equals("notice_form")) {
                        ResultDTO noticeformlist = noticeFormResource.getNoticeotherInfo("{notice_form_id:" + seresult.get(i)[2] + "}", "1", "1");
                        if (noticeformlist.getValues() != null && !noticeformlist.getValues().equals("null")) {
                            NoticeFormShowDTO noticeFormShowDTO = (NoticeFormShowDTO) noticeformlist.getValues().get(0);
                            BPManageShowDTO bpManageShowDTO = (BPManageShowDTO) bpmlist.getValues().get(0);
                            if (noticeFormShowDTO.getIsoutside().equals("1")) {
                                bpManageShowDTO.setName("收文登记");
                            } else {
                                bpManageShowDTO.setName("发文申请");
                            }
                            bpManageShowDTOs.add(0, bpManageShowDTO);
                        }

                    } else {
                        bpmlist = bpManagerResource.getBPManageInfo("{table_name:" + seresult.get(i)[3] + "}", "1", "1");
                    }

                    bp.setBpManageShowDTOList(bpManageShowDTOs);
                    result.add(bp);
                }


            }
            if (result.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");//
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");//
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    @Override
    public ResultDTO addWorkFlowInfo(WorkFlowPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

        String date = simpleDateFormat.format(new Date());

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String QUERY = "";

        QUERY += "insert into work_flow(work_flow_id,work_flow_formid,work_flow_type," +
                "work_flow_desc,work_flow_date,work_flow_order,work_flow_createid,work_flow_approval,work_flow_suggestion,work_flow_approval_organization_id,flow_path_id,work_flow_state,source_key) values" +
                " ('" + uuid + "','" + dataDTO.getWork_flow_formid() + "','" + dataDTO.getWork_flow_type() + "','"
                + dataDTO.getWork_flow_desc() + "','" + date + "','"
                + dataDTO.getWork_flow_order() + "','" + dataDTO.getWork_flow_createid() + "','" + dataDTO.getWork_flow_approval() + "','"
                + dataDTO.getWork_flow_suggestion() + "','" + dataDTO.getWork_flow_approval_organization_id() + "','" + dataDTO.getFlow_path_id()
                + "','0','" + dataDTO.getSource_key() + "')";

        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.add_success);

            //添加流程审核申请人记录
            WorkFlowHistoryPullDTO workFlowHistoryPullDTO = new WorkFlowHistoryPullDTO();
            workFlowHistoryPullDTO.setWork_flow_id(uuid);
            workFlowHistoryPullDTO.setWork_flow_formid(dataDTO.getWork_flow_formid());
            workFlowHistoryPullDTO.setWork_flow_approval(dataDTO.getWork_flow_createid());
            workFlowHistoryPullDTO.setWork_flow_type(dataDTO.getWork_flow_type());
            workFlowHistoryPullDTO.setWork_flow_suggestion(dataDTO.getWork_flow_suggestion());
            workFlowHistoryPullDTO.setWork_flow_date(date);
            workFlowHistoryPullDTO.setWork_flow_finish_date(date);
            workFlowHistoryPullDTO.setWork_flow_status("0");

            workFlowHistoryResource.addWorkFlowHistoryInfo(workFlowHistoryPullDTO);

            //添加流程审核审批人记录
            WorkFlowHistoryPullDTO workFlowHistory = new WorkFlowHistoryPullDTO();
            workFlowHistory.setWork_flow_id(uuid);
            workFlowHistory.setWork_flow_formid(dataDTO.getWork_flow_formid());
            workFlowHistory.setWork_flow_approval_organization_id(dataDTO.getWork_flow_approval_organization_id());
            workFlowHistory.setWork_flow_approval(dataDTO.getWork_flow_approval());
            workFlowHistory.setWork_flow_type(dataDTO.getWork_flow_type());
            workFlowHistory.setWork_flow_suggestion(dataDTO.getWork_flow_suggestion());
            workFlowHistory.setWork_flow_date(date);
            workFlowHistoryPullDTO.setWork_flow_status("0");
            workFlowHistoryPullDTO.setSource_key(dataDTO.getSource_key());

            workFlowHistoryResource.addWorkFlowHistoryInfo(workFlowHistory);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO updateWorkFlowInfo(WorkFlowPullDTO dataDTO) {
        ResultDTO resultDTO = new ResultDTO();

        //先获取当前处理人信息
        List<WorkFlowShowDTO> list = getWorkFlowInfo("{work_flow_formid:"+dataDTO.getWork_flow_formid()+"}","1","1").getValues();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(new Date());
        String QUERY = "";
        QUERY += "update work_flow set work_flow_date ='" + date + "'";

        //修改内容
//        if(null!=dataDTO.getWork_flow_createid()&&!"".equals(dataDTO.getWork_flow_createid())) {
//            QUERY += ",work_flow_createid = '" + dataDTO.getWork_flow_createid() + "'";
//        }

        if (null != dataDTO.getIsverify() && !"".equals(dataDTO.getIsverify())) {
            QUERY += ",work_flow_state = '" + dataDTO.getIsverify() + "'";
        }
        if (null != dataDTO.getIs_finish() && !"".equals(dataDTO.getIs_finish())) {
            QUERY += ",is_finish = '" + dataDTO.getIs_finish() + "'";
        }

        if (null != dataDTO.getWork_flow_approval()) {
            QUERY += ",work_flow_approval = '" + dataDTO.getWork_flow_approval() + "'";
        }

        if (null != dataDTO.getWork_flow_order() && !"".equals(dataDTO.getWork_flow_order())) {
            QUERY += ",work_flow_order = '" + dataDTO.getWork_flow_order() + "'";
        }
        if (null != dataDTO.getWork_flow_approval_organization_id()) {//work_flow_approval_organization_id
            QUERY += ",work_flow_approval_organization_id = '" + dataDTO.getWork_flow_approval_organization_id() + "'";
        }
        if (null != dataDTO.getSource_key() && !"".equals(dataDTO.getSource_key())) {
            QUERY += ",source_key = '" + dataDTO.getSource_key() + "'";
        }
//        if (null != dataDTO.getDesc() && !"".equals(dataDTO.getDesc())) {
        //  updatedesc(dataDTO);

        SuggestionPullDTO suggestionPullDTO = new SuggestionPullDTO();
        suggestionPullDTO.setDesc(dataDTO.getDesc());
        suggestionPullDTO.setForm_id(dataDTO.getWork_flow_formid());
        suggestionPullDTO.setProcess_suggestion(dataDTO.getDesc());//
        suggestionResource.addSuggestionInfo(suggestionPullDTO);
//        }


        //条件

        QUERY += " where 1=1";

        if (dataDTO.getWork_flow_id() != null && !dataDTO.getWork_flow_id().equals("")) {
            QUERY += " and work_flow_id = '" + dataDTO.getWork_flow_id() + "'";
        }

        if (dataDTO.getWork_flow_formid() != null && !dataDTO.getWork_flow_formid().equals("")) {
            QUERY += " and work_flow_formid = '" + dataDTO.getWork_flow_formid() + "'";
        }

        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
            //更新流程提交状态

            //更新当前步骤处理人记录，添加处理意见
            WorkFlowHistoryPullDTO workFlowHistoryPullDTO = new WorkFlowHistoryPullDTO();
            workFlowHistoryPullDTO.setWork_flow_id(list.get(0).getWork_flow_id());
            workFlowHistoryPullDTO.setWork_flow_formid(list.get(0).getWork_flow_formid());
            workFlowHistoryPullDTO.setWork_flow_approval_organization_id(list.get(0).getWork_flow_approval_organization_id());
            workFlowHistoryPullDTO.setWork_flow_approval(list.get(0).getWork_flow_approval());
            workFlowHistoryPullDTO.setWork_flow_type(list.get(0).getWork_flow_type());
            workFlowHistoryPullDTO.setWork_flow_suggestion(dataDTO.getDesc());
            workFlowHistoryPullDTO.setWork_flow_date(list.get(0).getWork_flow_date());
            workFlowHistoryPullDTO.setWork_flow_status("0");

            workFlowHistoryResource.updateWorkFlowHistoryInfo(workFlowHistoryPullDTO);

            //添加下一步流程审核记录
            WorkFlowHistoryPullDTO workFlowHistory = new WorkFlowHistoryPullDTO();
            workFlowHistory.setWork_flow_id(list.get(0).getWork_flow_id());
            workFlowHistory.setWork_flow_formid(dataDTO.getWork_flow_formid());
            workFlowHistory.setWork_flow_approval_organization_id(list.get(0).getWork_flow_approval_organization_id());
            workFlowHistory.setWork_flow_approval(dataDTO.getWork_flow_approval());
            workFlowHistory.setWork_flow_type(list.get(0).getWork_flow_type());
            workFlowHistory.setWork_flow_date(date);
            workFlowHistory.setWork_flow_status("0");
            workFlowHistory.setSource_key(dataDTO.getSource_key());

            workFlowHistoryResource.addWorkFlowHistoryInfo(workFlowHistory);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO updataapply(WorkFlowPullDTO dataDTO) {

        //先获取当前处理人信息
        List<WorkFlowShowDTO> list = getWorkFlowInfo("{work_flow_formid:"+dataDTO.getWork_flow_formid()+"}","1","1").getValues();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Remind_type = "",Remind_content="";
        ResultDTO resultDTO = new ResultDTO();//构建返回对象
        String QUERY = "";

        String type = dataDTO.getWork_flow_type();

        QUERY += "update " + dataDTO.getWork_flow_type() + " set isverify ='" + dataDTO.getIsverify() + "'";

        if (type.equals("expen_form")) {
            Remind_type="6";
            Remind_content="您有一条报销单审核消息";
            QUERY += " where expen_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("advance_form")) {
            Remind_content="您有一条预支费用单审核消息";
            Remind_type="5";
            QUERY += " where advance_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("officetools_form")) {
            Remind_type="1";
            Remind_content="您有办公用品申请单审核消息";
            QUERY += " where officetools_form_id = '" + dataDTO.getWork_flow_formid() + "'";

            if(dataDTO.getIsverify().equals("1")){//审批通过修改办公用品数量
                //修改办公用品数量
                String office_sql = "SELECT o.office_id,o.officetools_number,of.apply_type FROM officetype o " +
                        "JOIN officetools_form of ON o.office_form_id = of.officetools_form_id " +
                        "WHERE o.office_form_id = '" + dataDTO.getWork_flow_formid() + "'";

                List<String[]> office_result = new ArrayList<String[]>();
                office_result = getReportingTo(office_sql);

                for (int i=0;i<office_result.size();i++){
                    if(office_result.get(i)[3].equals("1")){//领用减库存

                        String office_update = "UPDATE officetools_manage SET officetools_number = officetools_number - " +
                                office_result.get(i)[2] + " WHERE officetools_id = '" + office_result.get(i)[1] + "'";

                        addReportingTo(office_update);

                    }else if(office_result.get(i)[3].equals("2")){//采购加库存

                        String office_update = "UPDATE officetools_manage SET officetools_number = officetools_number + " +
                                office_result.get(i)[2] + " WHERE officetools_id = '" + office_result.get(i)[1] + "'";

                        addReportingTo(office_update);
                    }
                }
            }
        } else if (type.equals("vacation_form")) {
            Remind_type="20";
            Remind_content="您有一条休假申请单审核消息";
            QUERY += " where vacation_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("vacation_form")) {
            Remind_type="2";
            Remind_content="您有一条休假申请单审核消息";
            QUERY += " where vacation_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("car_manage")) {
            Remind_type="7";
            Remind_content="您有一条车辆申请单审核消息";
            QUERY += " where car_manage_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("contract")) {
            Remind_type="8";
            Remind_content="您有一条合同申请单审核消息";
            QUERY += " where contract_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("meeting_room")) {
            Remind_type="4";
            Remind_content="您有一条会议室申请单审核消息";
            QUERY += " where meeting_room_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("finance_budget")) {
            Remind_type="3";
            Remind_content="您有一条财务预算申请单审核消息";
            QUERY += " where finance_budget_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("notice_form")) {
            Remind_content="您有一条收发文申请单审核消息";
            Remind_type="10";
            QUERY += " where notice_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("notice")) {
            Remind_type="9";
            Remind_content="您有一条公告申请单审核消息";
            QUERY += " where notice_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("siperintend")) {
            Remind_type="12";
            Remind_content="您有一条督办事宜申请单审核消息";
            QUERY += " where siperintend_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("assist_sth")) {
            Remind_content="您有一条协办事宜申请单审核消息";
            Remind_type="13";
            QUERY += " where siperintend_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("news_draft")) {
            Remind_type="14";
            Remind_content="您有一条新闻申请单审核消息";
            QUERY += " where news_draft_id = '" + dataDTO.getWork_flow_formid() + "'";
        }
        else if (type.equals("report_torepair")) {
            Remind_type="15";
            Remind_content="您有一条报修申请单审核消息";
            QUERY += " where report_torepair_id = '" + dataDTO.getWork_flow_formid() + "'";
        }
        else if (type.equals("apply_card")) {
            Remind_type="17";
            Remind_content="您有一条补卡申请单审核消息";
            QUERY += " where apply_card_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("bespeak_service")) {
            Remind_type="16";
            Remind_content="您有一条服务预约申请单审核消息";
            QUERY += " where bespeak_service_id = '" + dataDTO.getWork_flow_formid() + "'";
        }


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);

            dataDTO.setIsverify(dataDTO.getIsverify());
            //更新流程提交状态
            updateWorkFlowStatus(dataDTO);

            if (dataDTO.getIsverify().equals("1") || dataDTO.getIsverify().equals("-1")) {
                RemindPullDTO remindPullDTO = new RemindPullDTO();
                remindPullDTO.setRemind_content(Remind_content);
                remindPullDTO.setRemind_time(df.format(new Date()));
                remindPullDTO.setRemind_type(Remind_type);
                remindPullDTO.setRemind_form_id(dataDTO.getWork_flow_formid());
                remindPullDTO.setRemind_order("0");
                remindPullDTO.setCreate_time(df.format(new Date()));
                remindPullDTO.setRemind_endtime("");
                remindPullDTO.setRemind_member_id(dataDTO.getRemind_member_id());
                remindPullDTO.setRemind_starttime("");
                remindPullDTO.setRemind_descritpion("您有一条单据审批内容！");
                remindResource.addRemindInfo(remindPullDTO);
            }

            //结案更新审核记录
            WorkFlowHistoryPullDTO workFlowHistory = new WorkFlowHistoryPullDTO();
            workFlowHistory.setWork_flow_id(list.get(0).getWork_flow_id());
            workFlowHistory.setWork_flow_formid(dataDTO.getWork_flow_formid());
            workFlowHistory.setWork_flow_approval_organization_id(list.get(0).getWork_flow_approval_organization_id());
            workFlowHistory.setWork_flow_approval(list.get(0).getWork_flow_approval());
            workFlowHistory.setWork_flow_type(list.get(0).getWork_flow_type());
            workFlowHistory.setWork_flow_date(list.get(0).getWork_flow_date());
            workFlowHistory.setWork_flow_suggestion(dataDTO.getDesc());
            workFlowHistory.setWork_flow_status(dataDTO.getIsverify());
            workFlowHistoryResource.updateWorkFlowHistoryInfo(workFlowHistory);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 删除信息
     *
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO deleteWorkFlowInfo(WorkFlowPullDTO dataDTO) {

        //删除之前首先查询该表中是否有记录

        String sql = "select count(1) from work_flow WHERE work_flow_formid='" + dataDTO.getWork_flow_formid() + "'";
        String existadvance = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if (!existadvance.equals("0")) {
            String QUERY = "";
            QUERY += "delete from work_flow where work_flow_formid = '" + dataDTO.getWork_flow_formid() + "'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result != 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

            //删除审核流程历史记录
            WorkFlowHistoryPullDTO workFlowHistoryPullDTO = new WorkFlowHistoryPullDTO();
            workFlowHistoryPullDTO.setWork_flow_id(dataDTO.getWork_flow_formid());
            workFlowHistoryResource.deleteWorkFlowHistoryInfo(workFlowHistoryPullDTO);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }

    /**
     * 修改每个申请单的desc信息
     *
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO updatedesc(WorkFlowPullDTO dataDTO) {
        ResultDTO resultDTO = new ResultDTO();//构建返回对象
        String QUERY = "";

        String type = dataDTO.getWork_flow_type();
        QUERY += "update " + dataDTO.getWork_flow_type() + " set fail_resource ='" + dataDTO.getDesc() + "'";

        if (type.equals("expen_form")) {
            QUERY += " where expen_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("advance_form")) {
            QUERY += " where advance_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("officetools_form")) {
            QUERY += " where officetools_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("vacation_form")) {
            QUERY += " where vacation_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("car_manage")) {
            QUERY += " where car_manage_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("contract")) {
            QUERY += " where contract_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("meeting_room")) {
            QUERY += " where meeting_room_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("finance_budget")) {
            QUERY += " where finance_budget_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("notice_form")) {
            QUERY += " where notice_form_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("notice")) {
            QUERY += " where notice_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("siperintend")) {
            QUERY += " where siperintend_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("assist_sth")) {
            QUERY += " where siperintend_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("news_draft")) {
            QUERY += " where news_draft_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("report_torepair")) {
            QUERY += " where report_torepair_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("bespeak_service")) {
            QUERY += " where bespeak_service_id = '" + dataDTO.getWork_flow_formid() + "'";
        } else if (type.equals("apply_card")) {
            QUERY += " where apply_card_id = '" + dataDTO.getWork_flow_formid() + "'";
        }


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    public int updateWorkFlowStatus(WorkFlowPullDTO dataDTO){

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(new Date());
        String QUERY = "";
        QUERY += "update work_flow set work_flow_date ='" + date + "'";

        //修改内容
//        if(null!=dataDTO.getWork_flow_createid()&&!"".equals(dataDTO.getWork_flow_createid())) {
//            QUERY += ",work_flow_createid = '" + dataDTO.getWork_flow_createid() + "'";
//        }

        if (null != dataDTO.getIsverify() && !"".equals(dataDTO.getIsverify())) {
            QUERY += ",work_flow_state = '" + dataDTO.getIsverify() + "'";
        }
        if (null != dataDTO.getIs_finish() && !"".equals(dataDTO.getIs_finish())) {
            QUERY += ",is_finish = '" + dataDTO.getIs_finish() + "'";
        }

        if (null != dataDTO.getWork_flow_approval()) {
            QUERY += ",work_flow_approval = '" + dataDTO.getWork_flow_approval() + "'";
        }

        if (null != dataDTO.getWork_flow_order() && !"".equals(dataDTO.getWork_flow_order())) {
            QUERY += ",work_flow_order = '" + dataDTO.getWork_flow_order() + "'";
        }
        if (null != dataDTO.getWork_flow_approval_organization_id()) {//work_flow_approval_organization_id
            QUERY += ",work_flow_approval_organization_id = '" + dataDTO.getWork_flow_approval_organization_id() + "'";
        }
//        if (null != dataDTO.getDesc() && !"".equals(dataDTO.getDesc())) {
        //  updatedesc(dataDTO);

        SuggestionPullDTO suggestionPullDTO = new SuggestionPullDTO();
        suggestionPullDTO.setDesc(dataDTO.getDesc());
        suggestionPullDTO.setForm_id(dataDTO.getWork_flow_formid());
        suggestionPullDTO.setProcess_suggestion(dataDTO.getDesc());//
        suggestionResource.addSuggestionInfo(suggestionPullDTO);
//        }


        //条件

        QUERY += " where 1=1";

        if (dataDTO.getWork_flow_id() != null && !dataDTO.getWork_flow_id().equals("")) {
            QUERY += " and work_flow_id = '" + dataDTO.getWork_flow_id() + "'";
        }

        if (dataDTO.getWork_flow_formid() != null && !dataDTO.getWork_flow_formid().equals("")) {
            QUERY += " and work_flow_formid = '" + dataDTO.getWork_flow_formid() + "'";
        }


        return this.addReportingTo(QUERY);
    }
}
